/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts.action;

import com.myapp.bo.WantEsBO;
import com.myapp.struts.form.WantEstimateForm;
import com.myapp.util.DBConfig;
import com.myapp.util.HibernateUtil;
import java.sql.CallableStatement;
import java.sql.Connection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author toint
 * @since 12/2013
 * @version 1.0
 */
public class AddWantEsAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, 
            ActionForm form, HttpServletRequest req, 
            HttpServletResponse res) throws Exception {
//        Session session = HibernateUtil.getSessionFactory().openSession();
//        Transaction transaction = null;
        try {
            Connection conn = DBConfig.getConnection();
            res.setContentType("charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
            WantEstimateForm frm = (WantEstimateForm) form;
            WantEsBO want = new WantEsBO();
            String code = frm.getCode();
            String name = frm.getName();
            String description = frm.getDescription();
            Integer type = frm.getType();
            Integer wantId = frm.getWantId();
            Integer parent_id = 0;
            want.setCode(code);
            want.setName(name);
            if (wantId != -1)
                parent_id = wantId;
            want.setDescription(description);
            String sql = "insert into estimate_want values(?, ?, ?, ?, ?)";
//            sql += "'"+ code + "', ";
//            sql += "N'"+ name + "', ";
//            sql += "N'"+ description +"', ";
//            sql += type + ", ";
//            if (wantId != -1) sql += wantId + ")";
//            else sql += 0 + ")";
            CallableStatement cs = conn.prepareCall(sql);
            cs.setString(1, code);
            cs.setString(2, name);
            cs.setString(3, description);
            cs.setInt(4, type);
            cs.setInt(5, parent_id);
            cs.execute();
//            transaction = session.beginTransaction();
//            session.save(want);
//            transaction.commit();
        
        } catch (Exception ex) {
            //Log the exception
//            transaction.rollback();
            System.err.println(ex);
            return mapping.findForward("failed");
        } finally {
//            session.close();
        }
        
        return mapping.findForward("success");
    }
    
}
